package component.fy.com.common_module.baseAdapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class BaseViewHolder extends RecyclerView.ViewHolder {
    private View itemView;
    private SparseArray<View> views;//存放Item中的控件
    public BaseViewHolder(View itemView){
        super(itemView);
        this.itemView = itemView;
        views = new SparseArray<View>();
    }
    //供adapter调用，返回holder(没有使用这方法)
    public static <T extends BaseViewHolder> T getHolder(Context context, ViewGroup parent, int layoutId){
        return (T) new BaseViewHolder(LayoutInflater.from(context).inflate(layoutId, parent, false));
    }
    //根据Item中的控件Id获取控件
    public <T extends View> T getView(int viewId){
        View childreView = views.get(viewId);
        if (childreView == null){
            childreView = itemView.findViewById(viewId);
            views.put(viewId, childreView);
        }
        return (T) childreView;
    }

    public View getItemView() {
        return itemView;
    }
}
